package org.insia.smartposse.dao;

import java.util.ArrayList;
import java.util.List;

import org.insia.smartposse.config.DAOFactory;
import org.insia.smartposse.idao.IProjectSkillAffectationDAO;
import org.insia.smartposse.metier.ProjectSkillAffectation;
import org.insia.smartposse.metier.webservice.LightProjectSkillAffectation;
/**
 * DAO for ProjectSkillAffectation class.
 * @author sok hout
 *
 */
public class ProjectSkillAffectationDAO 
extends GenericDAO<ProjectSkillAffectation, Long>
implements IProjectSkillAffectationDAO {
	/**
	 * Empty constructor
	 *
	 */
	public ProjectSkillAffectationDAO() {
		type = ProjectSkillAffectation.class;
	}

	/* (non-Javadoc)
	 * @see org.insia.smartposse.idao.IProjectSkillAffectationDAO#createLighProjectSkillAffectation(org.insia.smartposse.metier.webservice.LightProjectSkillAffectation)
	 */
	public LightProjectSkillAffectation createLighProjectSkillAffectation(LightProjectSkillAffectation projectSkillAffectation) {
		try {
			ProjectSkillAffectation skillAffectation = new ProjectSkillAffectation();
			skillAffectation.setStart_date(projectSkillAffectation.getStart_date());
			skillAffectation.setEnd_date(projectSkillAffectation.getEnd_date());
			skillAffectation.setFreelance(DAOFactory.getFreelanceDAO().get(projectSkillAffectation.getFreelance()));
			skillAffectation.setProjectSkill(DAOFactory.getProjectSkillDAO().get(projectSkillAffectation.getProjectSkill()));
			projectSkillAffectation.setId(DAOFactory.getProjectSkillAffectationDAO().create(skillAffectation));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return projectSkillAffectation;
	}

	/* (non-Javadoc)
	 * @see org.insia.smartposse.idao.IProjectSkillAffectationDAO#createLighProjectSkillAffectation(java.util.List)
	 */
	public List<LightProjectSkillAffectation> createLighProjectSkillAffectation(List<LightProjectSkillAffectation> affectations) {
		List<LightProjectSkillAffectation> spsAffectation = new ArrayList<LightProjectSkillAffectation>();
		try {
			for (LightProjectSkillAffectation lpsAffectation : affectations) {
				ProjectSkillAffectation psAffectation = new ProjectSkillAffectation();
				psAffectation.setStart_date(lpsAffectation.getStart_date());
				psAffectation.setEnd_date(lpsAffectation.getEnd_date());
				psAffectation.setFreelance(DAOFactory.getFreelanceDAO().get(lpsAffectation.getFreelance()));
				psAffectation.setProjectSkill(DAOFactory.getProjectSkillDAO().get(lpsAffectation.getProjectSkill()));
				lpsAffectation.setId(DAOFactory.getProjectSkillAffectationDAO().create(psAffectation));
				spsAffectation.add(lpsAffectation);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return spsAffectation;
	}

	/*
	 * (non-Javadoc)
	 * @see org.insia.smartposse.idao.IProjectSkillAffectationDAO#updateLighProjectSkillAffectation(org.insia.smartposse.metier.webservice.LightProjectSkillAffectation)
	 */
	public LightProjectSkillAffectation updateLighProjectSkillAffectation(LightProjectSkillAffectation projectSkillAffectation) {
		try {
			ProjectSkillAffectation skillAffectation = DAOFactory.getProjectSkillAffectationDAO().get(projectSkillAffectation.getId());
			skillAffectation.setStart_date(projectSkillAffectation.getStart_date());
			skillAffectation.setEnd_date(projectSkillAffectation.getEnd_date());
			skillAffectation.setFreelance(DAOFactory.getFreelanceDAO().get(projectSkillAffectation.getFreelance()));
			skillAffectation.setProjectSkill(DAOFactory.getProjectSkillDAO().get(projectSkillAffectation.getProjectSkill()));
			DAOFactory.getProjectSkillAffectationDAO().update(skillAffectation);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return projectSkillAffectation;
	}

	/*
	 * (non-Javadoc)
	 * @see org.insia.smartposse.idao.IProjectSkillAffectationDAO#updateLighProjectSkillAffectation(java.util.List)
	 */
	public List<LightProjectSkillAffectation> updateLighProjectSkillAffectation(List<LightProjectSkillAffectation> affectations) {
		List<LightProjectSkillAffectation> spsAffectation = new ArrayList<LightProjectSkillAffectation>();
		try {
			for (LightProjectSkillAffectation lpsAffectation : affectations) {
				ProjectSkillAffectation psAffectation = DAOFactory.getProjectSkillAffectationDAO().get(lpsAffectation.getId());
				psAffectation.setStart_date(lpsAffectation.getStart_date());
				psAffectation.setEnd_date(lpsAffectation.getEnd_date());
				psAffectation.setFreelance(DAOFactory.getFreelanceDAO().get(lpsAffectation.getFreelance()));
				psAffectation.setProjectSkill(DAOFactory.getProjectSkillDAO().get(lpsAffectation.getProjectSkill()));
				DAOFactory.getProjectSkillAffectationDAO().update(psAffectation);
				spsAffectation.add(lpsAffectation);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return spsAffectation;
	}
}
